<template>
  <view class="content">
    <van-nav-bar
      title="店铺详情"
      :left-arrow="true"
      :border="false"
      placeholder
      custom-style="color: '#fff'"
      fixed
      @click-left="onClickLeft"
    />
    <view class="shop">
      <view class="shop-info">
        <view class="shop-img">
          <zero-lazy-load
            class="img-box"
            :image="info.member_head"
            loadingImg="https://images.tongzhuangkc.com/tz/default-good.png"
            errorImg="https://images.tongzhuangkc.com/tz/default-good.png"
            borderRadius="16"
            imgHeight="112"
          ></zero-lazy-load>
          <view class="img-icon" :class="info.vip_type === 1 ? 'vip' : info.vip_type === 2 ? 'active' : ''"></view>
        </view>
        <view class="info-box">
          <view class="title">{{ info.member_name }}</view>
          <view class="info-tag">已加入{{ info.shop_join_day }}天</view>
        </view>
      </view>
      <view class="shop-des">
        {{ info.member_desc }}
      </view>
      <view class="shop-opt">
        <view class="opt-btn btn-edit" v-if="info.is_owner === 1" @click="goEdit">编辑店铺</view>
        <button open-type="share" class="opt-btn">分享店铺</button>
      </view>
    </view>
    <view class="goods" id="goodsBox">
      <Scrollbox @lowerFun="getList" :otherHeight="topHeight" :status="loadingStatus">
        <template #contScrollBox>
          <view class="good-cont" style="display: flex; flex-wrap: wrap">
            <view class="good-item" v-for="item in list" :key="item.goods_id">
              <GoodList :info="item"></GoodList>
            </view>
          </view>
        </template>
      </Scrollbox>
    </view>
  </view>
</template>

<script>
import { reactive, toRefs } from 'vue'
import { onLoad, onShareAppMessage } from '@dcloudio/uni-app'
import GoodList from '@/components/good/index'
import { shopDetail } from '@/http/shop'
import { goodList } from '@/http/goods'
import Scrollbox from '@/components/scrollbox/index'
export default {
  components: {
    GoodList,
    Scrollbox,
  },
  setup() {
    const state = reactive({
      isOpen: false,
      memberId: '',
      info: {},
      list: [],
      topHeight: 0,
      isEnd: false,
      loadingStatus: 'loading',
      listQuery: {
        member_id: '',
        page: 1,
      },
    })
    const changeOption = (data) => {
      state.isOpen = data
    }
    const onClickLeft = () => {
      uni.navigateBack({
        delta: 1,
      })
    }
    const getDetail = async () => {
      const { data } = await shopDetail({
        member_id: state.memberId,
      })
      const query = uni.createSelectorQuery()
      query
        .select('#goodsBox')
        .boundingClientRect((res) => {
          state.topHeight = (res.top - 80) * 2
        })
        .exec()
      state.info = data
      state.listQuery.member_id = data.member_id
      state.isEnd = false
      state.listQuery.page = 1
      state.loadingStatus = 'loading'
      state.list = []
      getList()
    }
    const goEdit = () => {
      uni.navigateTo({
        url: `/subpackage-user/info/index`,
      })
    }
    const getList = async () => {
      if (state.isEnd) return
      try {
        let data = await goodList(state.listQuery)
        const { list, total_page } = data.data
        state.list = state.listQuery.page === 1 ? list : state.list.concat(list)

        if (list.length === 0 || total_page === state.listQuery.page) {
          state.loadingStatus = 'loadmore'
          state.isEnd = true
        } else {
          state.loadingStatus = 'loading'
          state.listQuery.page++
        }
      } catch (e) {
        state.isRefresh = false
        state.loading = false
      }
    }
    onShareAppMessage((res) => {
      return {
        title: state.info.member_name,
        imageUrl: state.info.member_head,
        path: `/subpackage-goods/shop/index?id=${state.memberId}`,
      }
    })
    onLoad((option) => {
      uni.showShareMenu({
        withShareTicket: true,
        menus: ['shareAppMessage', 'shareTimeline'],
      })
      state.memberId = option.id
      getDetail()
    })
    return {
      ...toRefs(state),
      changeOption,
      onClickLeft,
      goEdit,
      getList,
    }
  },
}
</script>

<style lang="scss">
.content {
  background: linear-gradient(132deg, #38acff 0%, #3863ff 100%);
  height: 100vh;
  :deep(.van-nav-bar) {
    background: none;
    .van-nav-bar__title {
      color: #fff;
    }
    :deep(.van-nav-bar__arrow) {
      color: #fff;
    }
  }
}
.goods {
  position: relative;
  background: #fff;
}
:deep(.screen-cross) {
  border-radius: 0 !important;
}
.good-cont {
  padding: 0 24rpx;
  display: flex;
  flex-wrap: wrap;
  margin: 0 -9rpx;
  background: #fff;
  overflow-y: auto;
  &.over {
    overflow-y: hidden;
  }
  .good-item {
    width: calc(50% - 18rpx);
    margin: 0 9rpx 24rpx;

    :deep(.item-box) {
      padding: 16rpx 14rpx 10rpx;
    }
  }
}
.good-select {
  padding: 0 10rpx 30rpx;
  margin-bottom: 10rpx;
  position: relative;
  .select-box {
    display: flex;
    span {
      display: block;
      min-width: 80rpx;
      text-align: center;
      padding: 5rpx 30rpx;
      font-size: 12rpx;
      margin-right: 20rpx;
      border-radius: 30rpx;
      background: #ededed;
    }
  }
}
.shop {
  border-radius: 32rpx 32rpx 0 0;
  background: #fff;
  padding: 16rpx 24rpx 32rpx;
  margin-top: 40rpx;
  .shop-info {
    margin-top: -32rpx;
    display: flex;
    .shop-img {
      position: relative;
      width: 112rpx;
      height: 112rpx;
      .img-icon {
        width: 40rpx;
        height: 40rpx;
        background: url('https://images.tongzhuangkc.com/tz/vip.png') no-repeat;
        background-size: 100% 100%;
        position: absolute;
        bottom: 0;
        right: 0;
        &.active {
          background: url('https://images.tongzhuangkc.com/tz/vip2.png') no-repeat;
          background-size: 100% 100%;
        }
      }
      .img-box {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .info-box {
      padding-top: 32rpx;
      margin-left: 16rpx;
      width: calc(100% - 128rpx);
      .title {
        font-size: 28rpx;
        font-weight: 500;
        color: #2b303b;
      }
      .info-tag {
        display: inline-block;
        font-size: 20rpx;
        line-height: 32rpx;
        padding: 0 8rpx;
        height: 32rpx;
        color: #fff;
        margin-top: 8rpx;
        background: linear-gradient(90deg, #ffb46f 0%, #ffc93f 100%);
        border-radius: 28rpx;
      }
    }
  }
  .shop-des {
    font-size: 24rpx;
    color: #555b66;
    padding: 24rpx 0 16rpx;
    line-height: 40rpx;
    min-height: 40rpx;
  }
  .shop-opt {
    .opt-btn {
      display: inline-block;
      line-height: 56rpx;
      height: 58rpx;
      background: #ffffff;
      border-radius: 28rpx;
      font-size: 24rpx;
      color: #3775f2;
      padding: 0 24rpx;
      border: 2rpx solid #3775f2;
      vertical-align: top;
      &.btn-edit {
        background: #3775f2;
        color: #fff;
        height: 56rpx;
        margin-right: 24rpx;
      }
    }
  }
}
</style>
